package utile

import (
	"fmt"
	"log"
	"logstash/logger"
	"net"
	"strconv"
	"time"
)

func FailOnError(err error, msg string) {
	if err != nil {
		logger.Errorf("%s:%s", msg, err)
		fmt.Sprintf("%s:%s", msg, err)
	}
}

func GetOutboundIP() net.IP {
	conn, err := net.Dial("udp", "8.8.8.8:80")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	localAddr := conn.LocalAddr().(*net.UDPAddr)
	return localAddr.IP
}

//获取毫秒时间戳，13位数
func GetMicroTime() (int64, string) {
	timestamp := time.Now().UnixNano() / 1e6
	return timestamp, strconv.FormatInt(timestamp, 13)
}

//获取秒时间戳，10位数
func GetTimestamp() (int64, string) {
	timestamp := time.Now().Unix()
	return timestamp, strconv.FormatInt(timestamp, 10)
}
